import React, { useState, createContext, useContext } from "react";

// ? 关键代码
const CountContext = createContext();

// 子组件
function Counter() {
    const count = useContext(CountContext); // 一句话就可以得到count
    return (<h2>{count}</h2>)
}

function Example() {
    const [count, setCount] = useState(0); // 数组结构

    return (
        <div>
            <p>You clicked {count} times</p>
            <button onClick={() => { setCount(count + 1) }}>click me</button>
            {/*===关键代码===*/}
            <CountContext.Provider value={count}>
                <Counter />
            </CountContext.Provider>
        </div>
    )
}

export default Example;


